<?php
/**
* Script that takes all apache traffic server variables and posts them to another server
* @author Mav Peri
* http://code.google.com/p/php-ats/
* This file needs to be deployed to the ATS installation. It will create a json encoded ATS feed and use http post 
* Requires php 5.2.10 and curl.
* you need to make sure the ini file is available
*/


//list of available traffic server variables
$variables['Node name'] = "proxy.node.hostname";
$variables['Objects served'] = "proxy.node.user_agents_total_documents_served";
$variables['Transactions per second'] = "proxy.node.user_agent_xacts_per_second";
$variables['Document hit rate'] = "proxy.node.cache_hit_ratio_avg_10s";
$variables['Bandwidth savings'] = "proxy.node.bandwidth_hit_ratio_avg_10s";
$variables['Cache percent free'] = "proxy.node.cache.percent_free";
$variables['Open origin server connections'] = "proxy.node.current_server_connections";
$variables['Open client connections'] = "proxy.node.current_client_connections";
$variables['Cache transfers in progress'] = "proxy.node.current_cache_connections";
$variables['Client throughput (Mbits/sec)'] = "proxy.node.client_throughput_out";
$variables['Transactions per second'] = "proxy.node.user_agent_xacts_per_second";
$variables['DNS lookups per second'] = "proxy.node.dns.lookups_per_second";
$variables['Host database hit rate'] = "proxy.node.hostdb.hit_ratio_avg_10s";
$variables['Total document bytes from client'] = "proxy.process.http.user_agent_response_document_total_size";
$variables['Total header bytes from client'] = "proxy.process.http.user_agent_response_header_total_size";
$variables['Total connections to client'] = "proxy.process.http.total_client_connections";
$variables['Client transactions in progress'] = "proxy.process.http.current_client_transactions";
$variables['Total document bytes from origin server'] = "proxy.process.http.origin_server_response_document_total_size";
$variables['Total header bytes from origin server'] = "proxy.process.http.origin_server_response_header_total_size";
$variables['Total connections to origin server'] = "proxy.process.http.total_server_connections";
$variables['Origin server transactions in progress'] = "proxy.process.http.current_server_transactions";
$variables['ICP query requests originating from this node'] = "proxy.process.icp.icp_query_requests";
$variables['ICP query messages sent from this node'] = "proxy.process.icp.total_udp_send_queries";
$variables['ICP peer hit messages received from this node'] = "proxy.process.icp.icp_query_hits";
$variables['ICP peer miss messages received from this node'] = "proxy.process.icp.icp_query_misses";
$variables['Total ICP responses received from this node'] = "proxy.process.icp.icp_remote_responses";
$variables['Average ICP message response time (ms) from this node'] = "proxy.process.icp.total_icp_response_time";
$variables['Average ICP request time (ms) from this node'] = "proxy.process.icp.total_icp_request_time";
$variables['Query messages received from ICP peers'] = "proxy.process.icp.icp_remote_query_requests";
$variables['Remote query hits from ICP peers'] = "proxy.process.icp.cache_lookup_success";
$variables['Remote query misses from ICP peers'] = "proxy.process.icp.cache_lookup_fail";
$variables['Successful response messages sent to peers'] = "proxy.process.icp.query_response_write";
$variables['Bytes used'] = "proxy.process.cache.bytes_used";
$variables['Cache size'] = "proxy.process.cache.bytes_total";
$variables['Lookups in progress'] = "proxy.process.cache.lookup.active";
$variables['Lookups completed'] = "proxy.process.cache.lookup.success";
$variables['Lookup misses'] = "proxy.process.cache.lookup.failure";
$variables['Reads in progress'] = "proxy.process.cache.read.active";
$variables['Reads completed'] = "proxy.process.cache.read.success";
$variables['Read misses'] = "proxy.process.cache.read.failure";
$variables['Writes in progress'] = "proxy.process.cache.write.active";
$variables['Writes completed'] = "proxy.process.cache.write.success";
$variables['Write failures'] = "proxy.process.cache.write.failure";
$variables['Updates in progress'] = "proxy.process.cache.update.active";
$variables['Updates completed'] = "proxy.process.cache.update.success";
$variables['Update failures'] = "proxy.process.cache.update.failure";
$variables['Removes in progress'] = "proxy.process.cache.remove.active";
$variables['Remove successes'] = "proxy.process.cache.remove.success";
$variables['Remove failures'] = "proxy.process.cache.remove.failur";
$variables['Total lookups'] = "proxy.process.hostdb.total_lookups";
$variables['Total hits'] = "proxy.process.hostdb.total_hits";
$variables['Time TTL (min)'] = "proxy.process.hostdb.ttl";
$variables['DNS total lookups'] = "proxy.process.dns.total_dns_lookups";
$variables['Average lookup time (msec)'] = "proxy.process.dns.lookup_avg_time";
$variables['DNS successes'] = "proxy.process.dns.lookup_successes";
$variables['Bytes read'] = "proxy.process.cluster.read_bytes";
$variables['Bytes written'] = "proxy.process.cluster.write_bytes";
$variables['Connections open'] = "proxy.process.cluster.connections_open";
$variables['Total operations'] = "proxy.process.cluster.connections_opened";
$variables['Network backups'] = "proxy.process.cluster.net_backup";
$variables['Clustering nodes'] = "proxy.process.cluster.nodes";
$variables['Log files currently open'] = "proxy.process.log.log_files_open";
$variables['Space used for log files'] = "proxy.process.log.log_files_space_used";
$variables['Number of access events logged'] = "proxy.process.log.event_log_access";
$variables['Number of access events skipped'] = "proxy.process.log.event_log_access_skip";
$variables['Number of error events logged'] = "proxy.process.log.event_log_error";
$variables['Number of congestions Traffic Server has observed because the maximum number of connections was exceeded.'] = "proxy.process.congestion.congested_on_max_connection";
$variables['Number of congestions Traffic Server has observed because of an OS response or timeout failure.'] = "proxy.process.congestion.congested_on_conn_failures";

$results = array();

$results['dataset creation']['value'] = date("Y-m-d H:i:s");
foreach($variables as $key=>$value){
	$out = array();
	exec('traffic_line -r'.$value, $out);
	if(is_array($out)){
		if(array_key_exists(0, $out)){
			$results[$key]['value'] = $out[0];
		}
	}
	unset($out);
}


//print_r($results);

// Post the results to a url

	
		$data = array(
				'x-meta-feed-type: traffic-server-status',
				'x-meta-feed-parameters: traffic-server-status',
				'x-meta-default-filename: traffic-serverl.xml',
				'x-meta-game-id: 1',
				'x-meta-competition-id: 1',
				'x-meta-season-id: 1',
				'x-meta-gamesystem-id: 1',
				'x-meta-matchday: 1',
				'x-meta-away-team-id: 1',
				'x-meta-home-team-id: 1',
				'x-meta-game-status: 1',
				'x-meta-language: en',
				'x-meta-production-server: traffic-server',
				'x-meta-production-server-timestamp: '.gmdate("Y-m-d H:i:s"),
				'x-meta-production-server-module: traffic-server',
				'Content-Type: JSON',
				'x-meta-encoding: UTF-8'
		);

	
	// Get the curl session object
	// you need to make sure the ini below is available
	$settings = parse_ini_file('/etc/php-ats.ini', true);
	$session = curl_init($settings['http']['write_endpoint']);

	// Tell curl to use HTTP POST;
	curl_setopt ($session, CURLOPT_POST, true);
	curl_setopt($session, CURLOPT_HTTPHEADER, $data); 
	
	// Tell curl that this is the body of the POST
	curl_setopt ($session, CURLOPT_POSTFIELDS, json_encode($results));
	// Tell curl not to return headers, and  return the response
	curl_setopt($session, CURLOPT_HEADER, true);
	curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($session, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
	
	
	// allow redirects 
	
	$response = curl_exec($session);
	
			if (curl_errno($session)) { 
				print "Error: " . curl_error($session); 
			} else { 
				// Show me the result 
				var_dump($response); 
			} 
	

	curl_close($session);	


?>